﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;
public partial class SystemManager_MenuOrder : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDate();
        }
    }

    private void BindDate()
    {
        List<SysFun> list = new SysFunManager().GetAllSysFun();
        foreach (SysFun item in list)
        {
            if (item.ParentNodeId == 0)
            {
                TreeNode tn = new TreeNode("&nbsp;&nbsp;" + item.DisplayName);
                tn.Value = item.NodeId.ToString();
                //tn.NavigateUrl = item.NodeURL;
                tn.ImageUrl = "~/imges/tree_yellow.jpg";
                InitTreeByList(tn);
                this.TreeView1.Nodes.Add(tn);
            }
        }
    }
    private void InitTreeByList(TreeNode TN)
    {
        List<SysFun> list = new SysFunManager().GetAllSysFun();
        foreach (SysFun item in list)
        {
            if (item.ParentNodeId.ToString() == TN.Value)
            {
                TreeNode tn = new TreeNode();
                tn.Text = "&nbsp;&nbsp;" + item.DisplayName;
                tn.Value = item.NodeId.ToString();
                //tn.NavigateUrl = item.NodeURL;
                tn.ImageUrl = "~/imges/tree_blue.jpg";
                TN.ChildNodes.Add(tn);

            }


        }
    }

    protected void btnUp_Click(object sender, EventArgs e)
    {
        int nodeId = Convert.ToInt32(this.TreeView1.SelectedNode.Value);
        SysFun sysB = new SysFunManager().GetSysFunByNodeId(nodeId);
        int displayOrder1 = sysB.DisplayOrder;
        int parentNodeId = sysB.ParentNodeId;
        SysFun sysA = new SysFunManager().GetSysFunByOrderIdAndParentId(displayOrder1 - 1, parentNodeId);
        SysFun fun1 = new SysFun();
        fun1.NodeId = nodeId;
        fun1.DisplayName = sysB.DisplayName;
        fun1.NodeURL = sysB.NodeURL;
        fun1.ParentNodeId = sysB.ParentNodeId;

        SysFun fun2 = new SysFun();
        fun2.NodeId = sysA.NodeId;
        fun2.DisplayName = sysA.DisplayName;
        fun2.NodeURL = sysA.NodeURL;
        fun2.ParentNodeId = sysA.ParentNodeId;
        int displayOrder2 = sysA.DisplayOrder;
        if (displayOrder1 == 1)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('已经到最上面了！')</script>");
            return;
        }
        else
        {
            displayOrder1 = displayOrder1 - 1;
            fun1.DisplayOrder = displayOrder1;

            displayOrder2 = displayOrder2 + 1;
            fun2.DisplayOrder = displayOrder2;
        }
        new SysFunManager().UpdateSysFun(fun1);//对选择的修改
        new SysFunManager().UpdateSysFun(fun2);
        TreeView1.Nodes.Clear();
        BindDate();
    }
    protected void btnDown_Click(object sender, EventArgs e)
    {
        int num = 0;
        int nodeId = Convert.ToInt32(this.TreeView1.SelectedNode.Value);
        string strnodeId = nodeId.ToString();
        if (strnodeId.Length != 3)
        {
            int parId = Convert.ToInt32(strnodeId.Substring(0, 3));
            num = new SysFunManager().GetAllSysFunByParId(parId).Count;
        }
        else
        {
            num = new SysFunManager().GetAllSysFunByParId(0).Count;
        }
        SysFun sysB = new SysFunManager().GetSysFunByNodeId(nodeId);
        int displayOrder1 = sysB.DisplayOrder;
        int parentNodeId = sysB.ParentNodeId;

        SysFun fun1 = new SysFun();
        fun1.NodeId = nodeId;
        fun1.DisplayName = sysB.DisplayName;
        fun1.NodeURL = sysB.NodeURL;
        fun1.ParentNodeId = sysB.ParentNodeId;
        SysFun sysA = new SysFunManager().GetSysFunByOrderIdAndParentId(displayOrder1 + 1, parentNodeId);
        SysFun fun2 = new SysFun();
        fun2.NodeId = sysA.NodeId;
        fun2.DisplayName = sysA.DisplayName;
        fun2.NodeURL = sysA.NodeURL;
        fun2.ParentNodeId = sysA.ParentNodeId;
        int displayOrder2 = sysA.DisplayOrder;
        if (displayOrder1 == num)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('已经到最下面了！')</script>");
            return;
        }
        else
        {
            displayOrder1 = displayOrder1 + 1;
            fun1.DisplayOrder = displayOrder1;

            displayOrder2 = displayOrder2 - 1;
            fun2.DisplayOrder = displayOrder2;
        }
        new SysFunManager().UpdateSysFun(fun1);//对选择的修改
        new SysFunManager().UpdateSysFun(fun2);
        TreeView1.Nodes.Clear();
        BindDate();
    }
}
